package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.Administrator;
import com.lyy.photovoltaic.common.utils.Base64Util;
import com.lyy.photovoltaic.common.utils.MyMD5Util;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.AdministratorMapper;
import com.lyy.photovoltaic.service.AdministratorService;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Service
public class AdministratorServiceImpl implements AdministratorService {
    @Autowired
    private AdministratorMapper administratorMapper;

    @Override
    public ResultVO checkLogin(String aId, String pwd) {
        boolean flag = false;
        //1.根据账号名查询用户信息
        Administrator administrator = administratorMapper.selectByPrimaryKey(aId);
        //2.判断
        if (administrator != null) {
            try {
                flag = MyMD5Util.validPassword(pwd, administrator.getaPassword());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (flag) {
                //如果登录验证成功，则需要生成token
                //使用jwt生成token字符串
                JwtBuilder builder = Jwts.builder();
                String token = builder.setSubject(administrator.getaName())        //jwt第二部分data
                        .setIssuedAt(new Date())                   //设置token的生成时间
                        .setId(administrator.getaId())                    //设置用户id为token id
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置token过期时间。一天
                        .signWith(SignatureAlgorithm.HS256, "fasdfkjnior342f3")
                        .compact();//设置加密方式及密码

                return new ResultVO(ResultCode.LOGIN_SUCCESS_ADMIN,token, administrator);
            }
        }
        return new ResultVO(ResultCode.LOGIN_FAILED, null);
    }

    @Override
    public ResultVO adminRegist(String aId, String pwd) {
        synchronized (this) {
            //1.查询账号是否已经被注册
            Administrator administrator = administratorMapper.selectByPrimaryKey(aId);
            //2.如果没有被注册则进行保存
            if (administrator == null) {
                try {
                    String md5pwd = MyMD5Util.getEncryptedPwd(pwd);
                    Administrator admin = new Administrator();
                    admin.setaId(aId);
                    admin.setaPassword(md5pwd);
                    int i = administratorMapper.insert(admin);
                    System.out.println(i);
                    if (i > 0) {
                        return new ResultVO(ResultCode.REGIST_SUCCESS, admin);
                    } else {
                        //return new ResultVO(10002, "注册失败", null);
                        return new ResultVO(ResultCode.REGIST_FAILED_Admin, null);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("23123");
            return new ResultVO(ResultCode.REGIST_FAILED, null);
        }
    }
}
